home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / aetypes.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  40.0 KB  |  696 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''aetypes - Python objects representing various AE types.'''
  5. from Carbon.AppleEvents import *
  6. import struct
  7. from types import *
  8. import string
  9.  
  10. def pack(*args, **kwargs):
  11.     pack = pack
  12.     import aepack
  13.     return pack(*args, **kwargs)
  14.  
  15.  
  16. def nice(s):
  17.     """'nice' representation of an object"""
  18.     if type(s) is StringType:
  19.         return repr(s)
  20.     else:
  21.         return str(s)
  22.  
  23.  
  24. class Unknown:
  25.     '''An uninterpreted AE object'''
  26.     
  27.     def __init__(self, type, data):
  28.         self.type = type
  29.         self.data = data
  30.  
  31.     
  32.     def __repr__(self):
  33.         return 'Unknown(%s, %s)' % (`self.type`, `self.data`)
  34.  
  35.     
  36.     def __aepack__(self):
  37.         return pack(self.data, self.type)
  38.  
  39.  
  40.  
  41. class Enum:
  42.     '''An AE enumeration value'''
  43.     
  44.     def __init__(self, enum):
  45.         self.enum = '%-4.4s' % str(enum)
  46.  
  47.     
  48.     def __repr__(self):
  49.         return 'Enum(%s)' % `self.enum`
  50.  
  51.     
  52.     def __str__(self):
  53.         return string.strip(self.enum)
  54.  
  55.     
  56.     def __aepack__(self):
  57.         return pack(self.enum, typeEnumeration)
  58.  
  59.  
  60.  
  61. def IsEnum(x):
  62.     return isinstance(x, Enum)
  63.  
  64.  
  65. def mkenum(enum):
  66.     if IsEnum(enum):
  67.         return enum
  68.     
  69.     return Enum(enum)
  70.  
  71.  
  72. class InsertionLoc:
  73.     
  74.     def __init__(self, of, pos):
  75.         self.of = of
  76.         self.pos = pos
  77.  
  78.     
  79.     def __repr__(self):
  80.         return 'InsertionLoc(%s, %s)' % (`self.of`, `self.pos`)
  81.  
  82.     
  83.     def __aepack__(self):
  84.         rec = {
  85.             'kobj': self.of,
  86.             'kpos': self.pos }
  87.         return pack(rec, forcetype = 'insl')
  88.  
  89.  
  90.  
  91. def beginning(of):
  92.     return InsertionLoc(of, Enum('bgng'))
  93.  
  94.  
  95. def end(of):
  96.     return InsertionLoc(of, Enum('end '))
  97.  
  98.  
  99. class Boolean:
  100.     '''An AE boolean value'''
  101.     
  102.     def __init__(self, bool):
  103.         self.bool = not (not bool)
  104.  
  105.     
  106.     def __repr__(self):
  107.         return 'Boolean(%s)' % `self.bool`
  108.  
  109.     
  110.     def __str__(self):
  111.         if self.bool:
  112.             return 'True'
  113.         else:
  114.             return 'False'
  115.  
  116.     
  117.     def __aepack__(self):
  118.         return pack(struct.pack('b', self.bool), 'bool')
  119.  
  120.  
  121.  
  122. def IsBoolean(x):
  123.     return isinstance(x, Boolean)
  124.  
  125.  
  126. def mkboolean(bool):
  127.     if IsBoolean(bool):
  128.         return bool
  129.     
  130.     return Boolean(bool)
  131.  
  132.  
  133. class Type:
  134.     '''An AE 4-char typename object'''
  135.     
  136.     def __init__(self, type):
  137.         self.type = '%-4.4s' % str(type)
  138.  
  139.     
  140.     def __repr__(self):
  141.         return 'Type(%s)' % `self.type`
  142.  
  143.     
  144.     def __str__(self):
  145.         return string.strip(self.type)
  146.  
  147.     
  148.     def __aepack__(self):
  149.         return pack(self.type, typeType)
  150.  
  151.  
  152.  
  153. def IsType(x):
  154.     return isinstance(x, Type)
  155.  
  156.  
  157. def mktype(type):
  158.     if IsType(type):
  159.         return type
  160.     
  161.     return Type(type)
  162.  
  163.  
  164. class Keyword:
  165.     '''An AE 4-char keyword object'''
  166.     
  167.     def __init__(self, keyword):
  168.         self.keyword = '%-4.4s' % str(keyword)
  169.  
  170.     
  171.     def __repr__(self):
  172.         return 'Keyword(%s)' % `self.keyword`
  173.  
  174.     
  175.     def __str__(self):
  176.         return string.strip(self.keyword)
  177.  
  178.     
  179.     def __aepack__(self):
  180.         return pack(self.keyword, typeKeyword)
  181.  
  182.  
  183.  
  184. def IsKeyword(x):
  185.     return isinstance(x, Keyword)
  186.  
  187.  
  188. class Range:
  189.     '''An AE range object'''
  190.     
  191.     def __init__(self, start, stop):
  192.         self.start = start
  193.         self.stop = stop
  194.  
  195.     
  196.     def __repr__(self):
  197.         return 'Range(%s, %s)' % (`self.start`, `self.stop`)
  198.  
  199.     
  200.     def __str__(self):
  201.         return '%s thru %s' % (nice(self.start), nice(self.stop))
  202.  
  203.     
  204.     def __aepack__(self):
  205.         return pack({
  206.             'star': self.start,
  207.             'stop': self.stop }, 'rang')
  208.  
  209.  
  210.  
  211. def IsRange(x):
  212.     return isinstance(x, Range)
  213.  
  214.  
  215. class Comparison:
  216.     '''An AE Comparison'''
  217.     
  218.     def __init__(self, obj1, relo, obj2):
  219.         self.obj1 = obj1
  220.         self.relo = '%-4.4s' % str(relo)
  221.         self.obj2 = obj2
  222.  
  223.     
  224.     def __repr__(self):
  225.         return 'Comparison(%s, %s, %s)' % (`self.obj1`, `self.relo`, `self.obj2`)
  226.  
  227.     
  228.     def __str__(self):
  229.         return '%s %s %s' % (nice(self.obj1), string.strip(self.relo), nice(self.obj2))
  230.  
  231.     
  232.     def __aepack__(self):
  233.         return pack({
  234.             'obj1': self.obj1,
  235.             'relo': mkenum(self.relo),
  236.             'obj2': self.obj2 }, 'cmpd')
  237.  
  238.  
  239.  
  240. def IsComparison(x):
  241.     return isinstance(x, Comparison)
  242.  
  243.  
  244. class NComparison(Comparison):
  245.     
  246.     def __init__(self, obj1, obj2):
  247.         Comparison.__init__(obj1, self.relo, obj2)
  248.  
  249.  
  250.  
  251. class Ordinal:
  252.     '''An AE Ordinal'''
  253.     
  254.     def __init__(self, abso):
  255.         self.abso = '%-4.4s' % str(abso)
  256.  
  257.     
  258.     def __repr__(self):
  259.         return 'Ordinal(%s)' % `self.abso`
  260.  
  261.     
  262.     def __str__(self):
  263.         return '%s' % string.strip(self.abso)
  264.  
  265.     
  266.     def __aepack__(self):
  267.         return pack(self.abso, 'abso')
  268.  
  269.  
  270.  
  271. def IsOrdinal(x):
  272.     return isinstance(x, Ordinal)
  273.  
  274.  
  275. class NOrdinal(Ordinal):
  276.     
  277.     def __init__(self):
  278.         Ordinal.__init__(self, self.abso)
  279.  
  280.  
  281.  
  282. class Logical:
  283.     '''An AE logical expression object'''
  284.     
  285.     def __init__(self, logc, term):
  286.         self.logc = '%-4.4s' % str(logc)
  287.         self.term = term
  288.  
  289.     
  290.     def __repr__(self):
  291.         return 'Logical(%s, %s)' % (`self.logc`, `self.term`)
  292.  
  293.     
  294.     def __str__(self):
  295.         if type(self.term) == ListType and len(self.term) == 2:
  296.             return '%s %s %s' % (nice(self.term[0]), string.strip(self.logc), nice(self.term[1]))
  297.         else:
  298.             return '%s(%s)' % (string.strip(self.logc), nice(self.term))
  299.  
  300.     
  301.     def __aepack__(self):
  302.         return pack({
  303.             'logc': mkenum(self.logc),
  304.             'term': self.term }, 'logi')
  305.  
  306.  
  307.  
  308. def IsLogical(x):
  309.     return isinstance(x, Logical)
  310.  
  311.  
  312. class StyledText:
  313.     '''An AE object respresenting text in a certain style'''
  314.     
  315.     def __init__(self, style, text):
  316.         self.style = style
  317.         self.text = text
  318.  
  319.     
  320.     def __repr__(self):
  321.         return 'StyledText(%s, %s)' % (`self.style`, `self.text`)
  322.  
  323.     
  324.     def __str__(self):
  325.         return self.text
  326.  
  327.     
  328.     def __aepack__(self):
  329.         return pack({
  330.             'ksty': self.style,
  331.             'ktxt': self.text }, 'STXT')
  332.  
  333.  
  334.  
  335. def IsStyledText(x):
  336.     return isinstance(x, StyledText)
  337.  
  338.  
  339. class AEText:
  340.     '''An AE text object with style, script and language specified'''
  341.     
  342.     def __init__(self, script, style, text):
  343.         self.script = script
  344.         self.style = style
  345.         self.text = text
  346.  
  347.     
  348.     def __repr__(self):
  349.         return 'AEText(%s, %s, %s)' % (`self.script`, `self.style`, `self.text`)
  350.  
  351.     
  352.     def __str__(self):
  353.         return self.text
  354.  
  355.     
  356.     def __aepack__(self):
  357.         return pack({
  358.             keyAEScriptTag: self.script,
  359.             keyAEStyles: self.style,
  360.             keyAEText: self.text }, typeAEText)
  361.  
  362.  
  363.  
  364. def IsAEText(x):
  365.     return isinstance(x, AEText)
  366.  
  367.  
  368. class IntlText:
  369.     '''A text object with script and language specified'''
  370.     
  371.     def __init__(self, script, language, text):
  372.         self.script = script
  373.         self.language = language
  374.         self.text = text
  375.  
  376.     
  377.     def __repr__(self):
  378.         return 'IntlText(%s, %s, %s)' % (`self.script`, `self.language`, `self.text`)
  379.  
  380.     
  381.     def __str__(self):
  382.         return self.text
  383.  
  384.     
  385.     def __aepack__(self):
  386.         return pack(struct.pack('hh', self.script, self.language) + self.text, typeIntlText)
  387.  
  388.  
  389.  
  390. def IsIntlText(x):
  391.     return isinstance(x, IntlText)
  392.  
  393.  
  394. class IntlWritingCode:
  395.     '''An object representing script and language'''
  396.     
  397.     def __init__(self, script, language):
  398.         self.script = script
  399.         self.language = language
  400.  
  401.     
  402.     def __repr__(self):
  403.         return 'IntlWritingCode(%s, %s)' % (`self.script`, `self.language`)
  404.  
  405.     
  406.     def __str__(self):
  407.         return 'script system %d, language %d' % (self.script, self.language)
  408.  
  409.     
  410.     def __aepack__(self):
  411.         return pack(struct.pack('hh', self.script, self.language), typeIntlWritingCode)
  412.  
  413.  
  414.  
  415. def IsIntlWritingCode(x):
  416.     return isinstance(x, IntlWritingCode)
  417.  
  418.  
  419. class QDPoint:
  420.     '''A point'''
  421.     
  422.     def __init__(self, v, h):
  423.         self.v = v
  424.         self.h = h
  425.  
  426.     
  427.     def __repr__(self):
  428.         return 'QDPoint(%s, %s)' % (`self.v`, `self.h`)
  429.  
  430.     
  431.     def __str__(self):
  432.         return '(%d, %d)' % (self.v, self.h)
  433.  
  434.     
  435.     def __aepack__(self):
  436.         return pack(struct.pack('hh', self.v, self.h), typeQDPoint)
  437.  
  438.  
  439.  
  440. def IsQDPoint(x):
  441.     return isinstance(x, QDPoint)
  442.  
  443.  
  444. class QDRectangle:
  445.     '''A rectangle'''
  446.     
  447.     def __init__(self, v0, h0, v1, h1):
  448.         self.v0 = v0
  449.         self.h0 = h0
  450.         self.v1 = v1
  451.         self.h1 = h1
  452.  
  453.     
  454.     def __repr__(self):
  455.         return 'QDRectangle(%s, %s, %s, %s)' % (`self.v0`, `self.h0`, `self.v1`, `self.h1`)
  456.  
  457.     
  458.     def __str__(self):
  459.         return '(%d, %d)-(%d, %d)' % (self.v0, self.h0, self.v1, self.h1)
  460.  
  461.     
  462.     def __aepack__(self):
  463.         return pack(struct.pack('hhhh', self.v0, self.h0, self.v1, self.h1), typeQDRectangle)
  464.  
  465.  
  466.  
  467. def IsQDRectangle(x):
  468.     return isinstance(x, QDRectangle)
  469.  
  470.  
  471. class RGBColor:
  472.     '''An RGB color'''
  473.     
  474.     def __init__(self, r, g, b):
  475.         self.r = r
  476.         self.g = g
  477.         self.b = b
  478.  
  479.     
  480.     def __repr__(self):
  481.         return 'RGBColor(%s, %s, %s)' % (`self.r`, `self.g`, `self.b`)
  482.  
  483.     
  484.     def __str__(self):
  485.         return '0x%x red, 0x%x green, 0x%x blue' % (self.r, self.g, self.b)
  486.  
  487.     
  488.     def __aepack__(self):
  489.         return pack(struct.pack('hhh', self.r, self.g, self.b), typeRGBColor)
  490.  
  491.  
  492.  
  493. def IsRGBColor(x):
  494.     return isinstance(x, RGBColor)
  495.  
  496.  
  497. class ObjectSpecifier:
  498.     '''A class for constructing and manipulation AE object specifiers in python.
  499.     
  500.     An object specifier is actually a record with four fields:
  501.     
  502.     key type    description
  503.     --- ----    -----------
  504.     
  505.     \'want\'  type    4-char class code of thing we want,
  506.             e.g. word, paragraph or property
  507.     
  508.     \'form\'  enum    how we specify which \'want\' thing(s) we want,
  509.             e.g. by index, by range, by name, or by property specifier
  510.     
  511.     \'seld\'  any which thing(s) we want,
  512.             e.g. its index, its name, or its property specifier
  513.     
  514.     \'from\'  object  the object in which it is contained,
  515.             or null, meaning look for it in the application
  516.     
  517.     Note that we don\'t call this class plain "Object", since that name
  518.     is likely to be used by the application.
  519.     '''
  520.     
  521.     def __init__(self, want, form, seld, fr = None):
  522.         self.want = want
  523.         self.form = form
  524.         self.seld = seld
  525.         self.fr = fr
  526.  
  527.     
  528.     def __repr__(self):
  529.         s = 'ObjectSpecifier(%s, %s, %s' % (`self.want`, `self.form`, `self.seld`)
  530.         if self.fr:
  531.             s = s + ', %s)' % `self.fr`
  532.         else:
  533.             s = s + ')'
  534.         return s
  535.  
  536.     
  537.     def __aepack__(self):
  538.         return pack({
  539.             'want': mktype(self.want),
  540.             'form': mkenum(self.form),
  541.             'seld': self.seld,
  542.             'from': self.fr }, 'obj ')
  543.  
  544.  
  545.  
  546. def IsObjectSpecifier(x):
  547.     return isinstance(x, ObjectSpecifier)
  548.  
  549.  
  550. class Property(ObjectSpecifier):
  551.     
  552.     def __init__(self, which, fr = None, want = 'prop'):
  553.         ObjectSpecifier.__init__(self, want, 'prop', mktype(which), fr)
  554.  
  555.     
  556.     def __repr__(self):
  557.         if self.fr:
  558.             return 'Property(%s, %s)' % (`self.seld.type`, `self.fr`)
  559.         else:
  560.             return 'Property(%s)' % `self.seld.type`
  561.  
  562.     
  563.     def __str__(self):
  564.         if self.fr:
  565.             return 'Property %s of %s' % (str(self.seld), str(self.fr))
  566.         else:
  567.             return 'Property %s' % str(self.seld)
  568.  
  569.  
  570.  
  571. class NProperty(ObjectSpecifier):
  572.     
  573.     def __init__(self, fr = None):
  574.         self.want = 'prop'
  575.         ObjectSpecifier.__init__(self, self.want, 'prop', mktype(self.which), fr)
  576.  
  577.     
  578.     def __repr__(self):
  579.         rv = 'Property(%s' % `self.seld.type`
  580.         if self.fr:
  581.             rv = rv + ', fr=%s' % `self.fr`
  582.         
  583.         if self.want != 'prop':
  584.             rv = rv + ', want=%s' % `self.want`
  585.         
  586.         return rv + ')'
  587.  
  588.     
  589.     def __str__(self):
  590.         if self.fr:
  591.             return 'Property %s of %s' % (str(self.seld), str(self.fr))
  592.         else:
  593.             return 'Property %s' % str(self.seld)
  594.  
  595.  
  596.  
  597. class SelectableItem(ObjectSpecifier):
  598.     
  599.     def __init__(self, want, seld, fr = None):
  600.         t = type(seld)
  601.         if t == StringType:
  602.             form = 'name'
  603.         elif IsRange(seld):
  604.             form = 'rang'
  605.         elif IsComparison(seld) or IsLogical(seld):
  606.             form = 'test'
  607.         elif t == TupleType:
  608.             (form, seld) = seld
  609.         else:
  610.             form = 'indx'
  611.         ObjectSpecifier.__init__(self, want, form, seld, fr)
  612.  
  613.  
  614.  
  615. class ComponentItem(SelectableItem):
  616.     _propdict = { }
  617.     _elemdict = { }
  618.     
  619.     def __init__(self, which, fr = None):
  620.         SelectableItem.__init__(self, self.want, which, fr)
  621.  
  622.     
  623.     def __repr__(self):
  624.         if not (self.fr):
  625.             return '%s(%s)' % (self.__class__.__name__, `self.seld`)
  626.         
  627.         return '%s(%s, %s)' % (self.__class__.__name__, `self.seld`, `self.fr`)
  628.  
  629.     
  630.     def __str__(self):
  631.         seld = self.seld
  632.         if type(seld) == StringType:
  633.             ss = repr(seld)
  634.         elif IsRange(seld):
  635.             (start, stop) = (seld.start, seld.stop)
  636.             if InstanceType == InstanceType:
  637.                 pass
  638.             elif InstanceType == type(stop):
  639.                 if self.__class__ == self.__class__:
  640.                     pass
  641.                 elif self.__class__ == stop.__class__:
  642.                     ss = str(start.seld) + ' thru ' + str(stop.seld)
  643.                 else:
  644.                     ss = str(seld)
  645.             else:
  646.                 ss = str(seld)
  647.         s = '%s %s' % (self.__class__.__name__, ss)
  648.         if self.fr:
  649.             s = s + ' of %s' % str(self.fr)
  650.         
  651.         return s
  652.  
  653.     
  654.     def __getattr__(self, name):
  655.         if self._elemdict.has_key(name):
  656.             cls = self._elemdict[name]
  657.             return DelayedComponentItem(cls, self)
  658.         
  659.         if self._propdict.has_key(name):
  660.             cls = self._propdict[name]
  661.             return cls(self)
  662.         
  663.         raise AttributeError, name
  664.  
  665.  
  666.  
  667. class DelayedComponentItem:
  668.     
  669.     def __init__(self, compclass, fr):
  670.         self.compclass = compclass
  671.         self.fr = fr
  672.  
  673.     
  674.     def __call__(self, which):
  675.         return self.compclass(which, self.fr)
  676.  
  677.     
  678.     def __repr__(self):
  679.         return '%s(???, %s)' % (self.__class__.__name__, `self.fr`)
  680.  
  681.     
  682.     def __str__(self):
  683.         return 'selector for element %s of %s' % (self.__class__.__name__, str(self.fr))
  684.  
  685.  
  686. template = "\nclass %s(ComponentItem): want = '%s'\n"
  687. exec template % ('Text', 'text')
  688. exec template % ('Character', 'cha ')
  689. exec template % ('Word', 'cwor')
  690. exec template % ('Line', 'clin')
  691. exec template % ('paragraph', 'cpar')
  692. exec template % ('Window', 'cwin')
  693. exec template % ('Document', 'docu')
  694. exec template % ('File', 'file')
  695. exec template % ('InsertionPoint', 'cins')
  696.